﻿using Basic.HrRemoteModel.Company.Model;
using Shop.Collect;
using Shop.ExtendService;
using Shop.Model.DB;
using Shop.Model.Shop;
using Shop.RemoteModel.Shop.Model;
using Shop.Service.Interface;
using WeDonekRpc.Helper;

namespace Shop.Service.lmpl
{
    internal class ShopService : IShopService
    {
        private readonly IShopCollect _Service;
        private readonly ICompanyService _Company;


        public ShopService (IShopCollect service,
            ICompanyService company)
        {
            this._Service = service;
            this._Company = company;
        }
        public ShopDatum Get (long id)
        {
            return this._Service.Get<ShopDatum>(id);
        }

        public bool Set (long id, ShopSetArg datum)
        {
            DBShop shop = this._Service.Get<DBShop>(id);
            return this._Service.Set(shop, new ShopSet
            {
                ShopName = datum.ShopName,
                Show = datum.Show,
                ShopLogo = datum.ShopLogo,
            });
        }

        public long ShopLogin (long companyId)
        {
            long shopId = this._Service.GetByCompanyId(companyId);
            if (shopId == 0)
            {
                CompanyDto com = this._Company.Get(companyId);
                return this._Service.Add(new ShopAdd
                {
                    CompanyId = companyId,
                    ShopName = com.ShortName.GetValueOrDefault(com.FullName)
                });
            }
            return shopId;
        }
    }
}
